<!-- 修改记录弹出框组件 -->
<template>
  <el-drawer
    v-model="visible"
    title="修改记录"
    direction="rtl"
    size="30%"
    :destroy-on-close="true"
    :loading="loading"
    class="record-drawer"
    :modal-class="'custom-modal'"
  >
    <div v-if="recordList.length > 0" class="record-timeline">
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in recordList"
          :key="index"
          :timestamp="formatTime(item.createTime)"
          placement="top"
          :hollow="true"
          type="primary"
        >
          <div class="record-item">
            <div class="record-header">
              <span class="record-title">{{
                item.operationType === 1
                  ? "新增人："
                  : item.operationType === 2
                    ? "修改人："
                    : "删除人："
              }}</span>
              <span class="record-value">{{ item.userName }}</span>
            </div>
            <div class="record-content">
              <span class="record-title">{{
                item.operationType === 1
                  ? "新增信息："
                  : item.operationType === 2
                    ? "修改信息："
                    : "删除信息："
              }}</span>
              <div>
                <div
                  v-for="items in item.content"
                  :key="items"
                  class="record-value"
                >
                  {{ items }}
                </div>
              </div>
            </div>
          </div>
        </el-timeline-item>
      </el-timeline>
    </div>
    <div v-else class="record-empty">暂无修改记录</div>
  </el-drawer>
</template>

<script lang="ts" setup>
import { reportApi } from "@/api/report-api";
import dayjs from "dayjs";
import { ElMessage } from "element-plus";
import { defineExpose, ref } from "vue";

interface RecordItem {
  content: string[]; // 修改信息数组
  operationType: 1 | 2 | 3; // 1：新增，2：修改，3：删除
  userName: string; // 操作人名称
  createTime: string; // 操作时间
}

interface OpenParams {
  dataId: number; // 数据ID
  type: number; // 类型
}

const visible = ref(false);
const loading = ref(false);
const recordList = ref<RecordItem[]>([]);

// 获取修改记录数据
const fetchRecordData = async (params: OpenParams) => {
  try {
    loading.value = true;
    const response = await reportApi.getTracerInfo({
      dataId: params.dataId,
      type: params.type,
      pageNum: 1,
      pageSize: 10
    });

    if (response.code === 0 && response.data) {
      // 转换接口数据为组件所需格式
      recordList.value = response.data.list
        .map(item => ({
          content: Array.isArray(item.content)
            ? item.content
            : [item.content || ""],
          operationType: item.operationType || 2,
          userName: item.userName || "",
          createTime: item.createTime || ""
        }))
        .sort((a, b) => {
          // 按时间倒序排序
          return dayjs(b.createTime).valueOf() - dayjs(a.createTime).valueOf();
        });
    } else {
      ElMessage.error(response.message || "获取修改记录失败");
    }
  } catch (error) {
    console.error("获取修改记录失败:", error);
    ElMessage.error("获取修改记录失败，请稍后重试");
  } finally {
    loading.value = false;
  }
};

// 格式化时间
const formatTime = (time: string) => {
  return dayjs(time).format("YYYY-MM-DD HH:mm:ss");
};

// 打开抽屉
const open = async (params: OpenParams) => {
  if (!params?.dataId) {
    ElMessage.warning("缺少必要参数");
    return;
  }
  visible.value = true;
  await fetchRecordData(params);
};

// 关闭抽屉
const close = () => {
  visible.value = false;
  recordList.value = []; // 清空数据
};

// 暴露方法给父组件
defineExpose({
  open,
  close
});
</script>

<style lang="scss">
.record-drawer {
  .el-drawer__header {
    padding: 16px 20px;
    border-bottom: 1px solid #e4e7ed;
    color: #333;

    .el-drawer__title {
      font-size: 16px;
      font-weight: 500;
    }
  }
  .el-drawer__body {
    padding: 0 !important;
  }
  .record-timeline {
    padding: 0 20px 20px;

    :deep(.el-timeline) {
      padding-left: 16px;
      position: relative;

      // &::before {
      //   content: "";
      //   position: absolute;
      //   left: 4px;
      //   top: 0;
      //   height: 14px;
      //   width: 2px;
      //   background-color: #e8e8e8;
      // }
    }

    :deep(.el-timeline-item__node--normal) {
      left: -1px;
      top: 16px;
      width: 12px;
      height: 12px;
      background-color: #1890ff;
      border: 2px solid #1890ff;
      position: relative;

      &::after {
        content: "";
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        width: 4px;
        height: 4px;
        background-color: #fff;
        border-radius: 50%;
      }
    }

    :deep(.el-timeline-item) {
      padding-bottom: 20px;

      &:last-child {
        .el-timeline-item__tail {
          display: none;
        }
      }
    }

    :deep(.el-timeline-item__tail) {
      left: 4px;
      border-left: 2px solid #e8e8e8;
      top: 16px;
      height: calc(100%);
    }

    :deep(.el-timeline-item__timestamp) {
      color: #666;
      font-size: 14px;
      margin-bottom: 12px;
    }

    .record-item {
      background: #ffffff;
      border-radius: 8px;
      padding: 16px;
      margin-bottom: 8px;
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);

      .record-header,
      .record-content {
        margin-bottom: 12px;
        line-height: 1.6;
        display: flex;
        flex-direction: row;
        align-items: flex-start;

        &:last-child {
          margin-bottom: 0;
        }
      }

      .record-title {
        color: #666666;
        font-weight: 500;
        margin-right: 12px;
        min-width: 70px;
      }

      .record-value {
        color: #333;
        flex: 1;
        word-break: break-all;
        line-height: 1.6;
      }
    }
  }
}

:deep(.custom-modal) {
  background-color: rgba(0, 0, 0, 0.5);
}
.record-empty {
  text-align: center;
  color: #999;
  font-size: 14px;
  padding: 20px;
}
</style>
